﻿using ModuleBase.googoltech.GTS;
using ModuleBase;
using Prism.Ioc;
using System.Diagnostics;

namespace AxisSerOn
{
    public class AxisSerOn : ModuleBase.ModuleBase
    {
        private IAxisCardService _axisCardService;
        IContainerProvider _containerProvider;
        bool isExecuted = false;
        bool outputs = false;

        private string _parameter1;
        public string Parameter1
        {
            get => _parameter1;
            set
            {
                if (_parameter1 != value)
                {
                    _parameter1 = value;
                    OnPropertyChanged(nameof(Parameter1));
                }
            }
        }
        private string _parameter2 = "ON";
        public string Parameter2
        {
            get => _parameter2;
            set
            {
                if (_parameter2 != value)
                {
                    _parameter2 = value;
                    OnPropertyChanged(nameof(Parameter2));
                }
            }
        }
        private string _parameter3 = "Top_A8";
        public string Parameter3
        {
            get => _parameter3;
            set
            {
                if (_parameter3 != value)
                {
                    _parameter3 = value;
                    OnPropertyChanged(nameof(Parameter3));
                }
            }
        }
        public override Dictionary<string, object> GetProperties()
        {
            return new Dictionary<string, object>
            {
                { "轴号", Parameter1 },
                { "使能", Parameter2 },
                { "轴卡参数名", Parameter3 },
            };
        }

        public override Dictionary<string, List<object>> PropertyOptions { get; } = new Dictionary<string, List<object>>
        {
            { "使能", new List<object> {  "ON","OFF" } },
        };

        public AxisSerOn(IContainerProvider containerProvider)
        {
            _containerProvider = containerProvider;
            Name = "轴使能";

            DisplayNameToPropertyNameMap = new Dictionary<string, string>
            {
                { "轴号", nameof(Parameter1) },
                { "使能", nameof(Parameter2) },
                { "轴卡参数名", nameof(Parameter3) },
            };
        }

        public override bool Execute()
        {
            Status = ModuleStatus.Executing; // 设置状态为执行中

            if (isExecuted)
            {
                outputs = true;
                return outputs;
            }

            _axisCardService = _containerProvider.Resolve<IAxisCardService>(Parameter3);

            var param = Parameter1.ToString().Split(',', '，');
            if (param != null && param.Length > 0)
            {
                param.ToList().ForEach(p =>
                {
                    AxisParm _axis = GetAxisByNumber(int.Parse(p));
                    try
                    {
                        if (Parameter2 == "ON")
                        {
                            GTSCard.ServoOn(GetAxisByNumber(int.Parse(p)));
                        }
                        else
                        {
                            GTSCard.ServoOff(GetAxisByNumber(int.Parse(p)));
                        }
                        Debug.WriteLine($"轴{int.Parse(p)}使能已：{Parameter2}");
                        outputs = true;
                        isExecuted = true;
                    }
                    catch
                    {
                        outputs = false;
                    }
                });
            }
            else
            {
                outputs = false;
            }
            Thread.Sleep(1000);
            return outputs;
        }

        public override async Task<bool> ExecuteAsync()
        {
            outputs = false;
            var result = await Task.Run(() => Execute());
            return result;
        }

        public override void Initialize()
        {

        }
        private AxisParm GetAxisByNumber(int axisNumber)
        {
            switch (axisNumber)
            {
                case 1: return _axisCardService.A1;
                case 2: return _axisCardService.A2;
                case 3: return _axisCardService.A3;
                case 4: return _axisCardService.A4;
                case 5: return _axisCardService.A5;
                case 6: return _axisCardService.A6;
                case 7: return _axisCardService.A7;
                case 8: return _axisCardService.A8;
                default: return _axisCardService.A1;
            }
        }

        public override string Description => "轴使能模块：用于批量控制指定轴的使能（ON/OFF）状态。";
    }
}
